package cc.rengu.igas.channel.xmup.core.service.trans;


import cc.rengu.igas.channel.xmup.common.constant.XmupConfigConstant;
import cc.rengu.igas.channel.xmup.common.constant.XmupParamConstant;
import cc.rengu.igas.channel.xmup.common.constant.XmupTreeNodeConstant;
import cc.rengu.igas.channel.xmup.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.xmup.common.enums.XmupProdEnum;
import cc.rengu.igas.channel.xmup.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.xmup.core.service.base.PayService;
import cc.rengu.igas.channel.xmup.facade.request.CreditTransStateQueryRequest;
import cc.rengu.igas.channel.xmup.facade.response.CreditTransStateQueryRespone;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 贷记交易状态查询服务 on 2019/10/8.
 */
public class CreditTransStateQueryService extends PayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        CreditTransStateQueryRequest transStateQueryRequest = new CreditTransStateQueryRequest();
        ConvertUtil.convertOutput(transStateQueryRequest);
        return transStateQueryRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        CreditTransStateQueryRespone creditTransStateQueryRespone = new CreditTransStateQueryRespone();
        CreditTransStateQueryRequest transStateQueryRequest = (CreditTransStateQueryRequest) request;
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(XmupTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        ConvertUtil.convertInput(transStateQueryRequest);
        xmlTreeUtil.setXmlTreeStringValue(XmupTreeNodeConstant.txnType, "00");
        xmlTreeUtil.setXmlTreeStringValue(XmupTreeNodeConstant.txnSubType, "00");
        xmlTreeUtil.setXmlTreeStringValue(XmupTreeNodeConstant.bizType, XmupProdEnum.PAYMENT.getProdCode());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, transStateQueryRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        if (98 == iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            creditTransStateQueryRespone.setRespCode("04");
            creditTransStateQueryRespone.setRespDesc("交易状态未明");
            chnlBizResponse.setResult(creditTransStateQueryRespone);
            return chnlBizResponse;
        } else if (0 != iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(creditTransStateQueryRespone);

        if (XmupParamConstant.SUCCESS_CODE.equals(creditTransStateQueryRespone.getRespCode())) {
            creditTransStateQueryRespone.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            creditTransStateQueryRespone.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            if (creditTransStateQueryRespone.getPayAcctNo() != null) {
                SysParamService sysParamService = new SysParamServiceImpl();
                SysParam sysParam = sysParamService.getSysParamInfo(transStateQueryRequest.getHeader().getInstId(),
                        XmupConfigConstant.XMUP_CONF_NODE, XmupConfigConstant.XMUP_ACCT_ENC);
                if (sysParam == null) {
                    rglog.error("未配置账户是否加密，不处理");
                } else if ("Y".equals(sysParam.getParamValue())) {
                    rglog.error("配置账户加密，解密账号");
//                    creditTransStateQueryRespone.setPayAcctNo(descryptData(creditTransStateQueryRespone.getPayAcctNo()));
                }
            }
        }
        chnlBizResponse.setResult(creditTransStateQueryRespone);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        CreditTransStateQueryRespone creditTransStateQueryRespone = (CreditTransStateQueryRespone) response.getResult();
        ConvertUtil.convertInput(creditTransStateQueryRespone);
    }


}
